home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Utilities / DVIM72-Mac 1.9.6 / source / half_bits.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-14  |  3.6 KB  |  122 lines  |  [TEXT/R*ch]

  1. /* Half_bits
  2.  
  3.     This routine is for scaling a bitmap by a factor of 1/2.
  4.     
  5.     Each pixel in the destination is determined by a group of 4 pixels
  6.     in the source.  If two adjacent source pixels are black, then
  7.     the destination pixel will be black.
  8.     
  9.     CopyBits makes the destination pixel black if any of the source
  10.     pixels is black.
  11. */
  12.  
  13. #define        nil        0L
  14. #include <MacProto.h>
  15.  
  16. void Half_bits(
  17.     BitMap    *src_bits,
  18.     BitMap    *dst_bits,
  19.     Rect    *src_rect,
  20.     Point    dst_topLeft )
  21. {
  22.     BitMap    half_band1;
  23.     register int    row;
  24.     int        num_bands;
  25.     Rect    s_rect, d_rect;
  26.     GrafPtr    save_port;
  27.     GrafPort    band_port;
  28.     Pattern        ll_lr_pat, ul_pat, ur_pat;
  29.  
  30.     GetPort( &save_port );
  31.     
  32.     /* Create an offscreen GrafPort for a 32-px-deep band. */
  33.     OpenPort( &band_port );
  34.     /*
  35.         The default visRgn is screenBits.bounds, which is
  36.         too small for printing.
  37.     */
  38.     CopyRgn( band_port.clipRgn, band_port.visRgn );
  39.     band_port.portBits.rowBytes = (src_rect->right - src_rect->left) / 8;
  40.     /* Make sure it's a multiple of 4 */
  41.     band_port.portBits.rowBytes = 4 *
  42.         ( (band_port.portBits.rowBytes + 3) / 4 );
  43.     SetRect( &band_port.portBits.bounds,
  44.         src_rect->left, 0, src_rect->right, 32 );
  45.     band_port.portBits.baseAddr =
  46.         NewPtr( 32 * band_port.portBits.rowBytes );
  47.     band_port.portRect = band_port.portBits.bounds;
  48.     PenMode( notSrcBic );    /* AND the paint pattern with the bitmap */
  49.     
  50.     half_band1.rowBytes = band_port.portBits.rowBytes / 2;
  51.     /* Make sure it's a multiple of 4 */
  52.     half_band1.rowBytes =
  53.         4 * ( (half_band1.rowBytes + 3) / 4 );
  54.     half_band1.bounds.left = half_band1.bounds.top = 0;
  55.     half_band1.bounds.right =
  56.         (band_port.portBits.bounds.right - band_port.portBits.bounds.left)
  57.         / 2;
  58.     half_band1.bounds.bottom = 16;
  59.     half_band1.baseAddr = NewPtr( 16 * half_band1.rowBytes );
  60.     
  61.     if ( (band_port.portBits.baseAddr != nil) &&
  62.         (half_band1.baseAddr != nil)  )
  63.     {
  64.         StuffHex( ll_lr_pat, "\p00FF00FF00FF00FF" );
  65.         StuffHex( ul_pat, "\pAA00AA00AA00AA00" );
  66.         StuffHex( ur_pat, "\p5500550055005500" );
  67.         num_bands = (src_rect->bottom - src_rect->top) / 32;
  68.         s_rect = *src_rect;
  69.         s_rect.bottom = s_rect.top + 32;
  70.         topLeft(d_rect) = dst_topLeft;
  71.         d_rect.bottom = d_rect.top + 16;
  72.         d_rect.right = d_rect.left + 
  73.             (src_rect->right - src_rect->left) / 2;
  74.         for (row = 0; row < num_bands; row++)
  75.         {
  76.             CopyBits( src_bits, &band_port.portBits,
  77.                 &s_rect, &band_port.portBits.bounds,
  78.                 srcCopy, nil );
  79.             PenPat( &ul_pat );
  80.             PaintRect(  &band_port.portBits.bounds );
  81.             CopyBits( &band_port.portBits, &half_band1,
  82.                 &band_port.portBits.bounds, &half_band1.bounds,
  83.                 srcCopy, nil );
  84.             CopyBits( src_bits, &band_port.portBits,
  85.                 &s_rect, &band_port.portBits.bounds,
  86.                 srcCopy, nil );
  87.             PenPat( &ur_pat );
  88.             PaintRect(  &band_port.portBits.bounds );
  89.             CopyBits( &band_port.portBits, &half_band1,
  90.                 &band_port.portBits.bounds, &half_band1.bounds,
  91.                 notSrcBic, nil );
  92.             /* Now half_band1 contains ul & ur */
  93.             
  94.             CopyBits( src_bits, &band_port.portBits,
  95.                 &s_rect, &band_port.portBits.bounds,
  96.                 srcCopy, nil );
  97.             PenPat( &ll_lr_pat );
  98.             PaintRect(  &band_port.portBits.bounds );
  99.             CopyBits( &band_port.portBits, &half_band1,
  100.                 &band_port.portBits.bounds, &half_band1.bounds,
  101.                 srcOr, nil );
  102.             
  103.             CopyBits( &half_band1, dst_bits,
  104.                 &half_band1.bounds, &d_rect, srcCopy, nil );
  105.             OffsetRect( &s_rect, 0, 32 );
  106.             OffsetRect( &d_rect, 0, 16 );
  107.         }
  108.         DisposPtr( band_port.portBits.baseAddr );
  109.         DisposPtr( half_band1.baseAddr );
  110.     }
  111.     else
  112.     {
  113.         topLeft(s_rect) = dst_topLeft;
  114.         s_rect.right = s_rect.left + (src_rect->right - src_rect->left) /2;
  115.         s_rect.bottom = s_rect.top + (src_rect->bottom - src_rect->top) /2;
  116.         CopyBits( src_bits, dst_bits, src_rect, &s_rect, srcCopy, nil );
  117.     }
  118.     
  119.     SetPort( save_port );
  120.     ClosePort( &band_port );
  121. }
  122.